var api = require("../../api.js");
var app = getApp();
import {
  $Login
} from '../../components/base.js'
let col1H = 0;
let col2H = 0;
const {
  $Toast
} = require('../../iview/base/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    community: {},
    areas: [],
    artsData: [],
    prosData: [],
    activeData: [],
    page_index: 1,
    page_size: 10,
    communityId: '',
    isFollowComm: '',
    /** 到底的提示 */
    loading: false,
    isBottom: false,
    scrollH: 0,
    imgWidth: 0,
    loadingCount: 0,
    images: [],
    col1: [],
    col2: [],
    currentTab: 0,
    type: 'all',
    /** 分享人的id  */
    shareId: '',
    /** 用户id  */
    open_id: '',
    /** 社群相册 */
    comm_albums: [],
    imgarr: []
  },

  /** 获取数据 */
  get_data: function () {
    let that = this;
    let sendData = {
      id: that.data.communityId,
      type: that.data.type,
      page_index: that.data.page_index,
      page_size: that.data.page_size
    }
    wx.request({
      url: api.community.comm_areas,
      data: sendData,
      header: {
        "content-type": "application/json"
      },
      method: 'GET',
      success(res) {
        setTimeout(() => {
          console.log(res)
          let baseId = "img-" + (+new Date());
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].id = baseId + "-" + i;
          }
          let areas = that.data.areas
          if (res.data.data.length > 0 && res.data.data.length === that.data.page_size) {
            res.data.data.map((item, index) => {
              areas.push(item)
            })
            that.setData({
              loadingCount: res.data.data.length,
              images: res.data.data,
              page_index: that.data.page_index + 1,
              areas: areas,
            })
          } else if (res.data.data.length > 0 && res.data.data.length < that.data.page_size) {
            let areas = that.data.areas
            res.data.data.map((item, index) => {
              areas.push(item)
            })
            that.setData({
              loading: true,
              isBottom: true,
              loadingCount: res.data.data.length,
              images: res.data.data,
              areas: areas,
              page_index: that.data.page_index + 1
            })
          } else {
            that.setData({
              loading: true,
              isBottom: true,
            })
          }
          wx.hideLoading()
        }, 500)
      }
    });
  },

  /** 加载数据 */
  loadMoreData: function () {
    let that = this
    wx.showLoading({
      title: "数据加载中"
    });
    if (that.data.isBottom) {
      wx.hideLoading()
      return false
    }
    /** 全部数据 */
    if (this.data.currentTab == 0) {
      that.setData({
        type: 'all'
      })
      this.get_data();
    }
    /** 文章数据 */
    else if (this.data.currentTab == 1) {
      that.setData({
        type: '0'
      })
      this.get_data();
    }
    /** 产品信息列表数据 */
    else if (this.data.currentTab == 2) {
      that.setData({
        type: '2'
      })
      this.get_data();
    } else if (this.data.currentTab == 3) {
      /** 全部活动数据 */
      let sendData = {
        pageIndex: this.data.page_index,
        pageSize: this.data.page_iize,
        community_id: this.data.communityId
      }
      /** 社群下活动数据拉取 */
      wx.request({
        url: api.community.activesList,
        data: sendData,
        header: {
          "content-type": "application/json"
        },
        method: 'GET',
        success(res) {
          setTimeout(() => {
            let activeData = that.data.activeData;
            if (res.data.data.length > 0 && res.data.data.length < that.data.page_size) {
              res.data.data.map(item => {
                activeData.push(item)
              })
              that.setData({
                loading: true,
                isBottom: true,
                activeData: activeData
              })
            } else if (res.data.data.length > 0 && res.data.data.length == that.data.page_size) {
              res.data.data.map(item => {
                activeData.push(item)
              })
              that.setData({
                activeData: activeData
              })
            } else {
              that.setData({
                loading: true,
                isBottom: true
              })
            }
            that.setData({
              page_index: that.data.page_index + 1
            })
            wx.hideLoading()
          }, 500)
        }
      });
    }
    /** 相册功能 */
    else if (this.data.currentTab == 4) {
      this.get_comm_albums();
    }
  },
  /** 获取社群相册 */
  get_comm_albums() {
    let _this = this
    /** 社群下社群相册数据 */
    wx.request({
      url: api.community.comm_albums,
      data: {
        id: this.data.communityId
      },
      method: 'GET',
      success(res) {
        console.log(res.data.data)
        setTimeout(() => {
          _this.setData({
            comm_albums: res.data.data
          })
          wx.hideLoading()
        }, 500)
      }
    });
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('分享的用户', options.shareId)
    console.log('id用户', )
    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let wh = res.windowHeight;
        let imgWidth = ww * 350 / 750;
        let scrollH = wh;
        this.setData({
          scrollH: scrollH,
          imgWidth: imgWidth,
          communityId: options.communitiesId,
          open_id: app.globalData.wxUserInfo.openId
        });
        if (options.shareId != undefined) {
          this.setData({
            shareId: options.shareId
          })
        }
        if (app.globalData.wxUserInfo.openId != undefined) {
          this.setData({
            open_id: app.globalData.wxUserInfo.openId
          })
        }
        //加载首组图片
        this.loadMoreData()
      }
    })
    this.initData();
  },

  onImageLoad: function (e) {
    let imageId = e.currentTarget.id;
    let oImgW = e.detail.width; //图片原始宽度
    let oImgH = e.detail.height; //图片原始高度
    let imgWidth = this.data.imgWidth; //图片设置的宽度
    let scale = imgWidth / oImgW; //比例计算
    let imgHeight = oImgH * scale; //自适应高度

    let images = this.data.images;
    let imageObj = null;

    for (let i = 0; i < images.length; i++) {
      let img = images[i];
      if (img.id === imageId) {
        imageObj = img;
        break;
      }
    }
    imageObj.height = imgHeight;
    let loadingCount = this.data.loadingCount - 1;
    let areas = this.data.areas;
    let data = {
      loadingCount: loadingCount,
      areas: areas
    };
    //当前这组图片已加载完毕，则清空图片临时加载区域的内容
    if (!loadingCount) {
      data.images = [];
    }
    this.setData(data);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /** 页面初始化 */
  initData: function () {
    wx.showLoading({
      title: "数据加载中"
    });
    let that = this;
    let sendData = {
      open_id: app.globalData.wxUserInfo.openId,
      id: this.data.communityId,
      share_id: this.data.shareId
    }
    /** 社群详情接口数据请求 */
    wx.request({
      url: api.community.comm_detail,
      data: sendData,
      header: {
        "content-type": "application/json"
      },
      method: 'GET',
      success(res) {
        setTimeout(() => {
          console.log('社群详情数据',res.data.data.community)
          wx.hideLoading()
          that.setData({
            community: res.data.data.community,
            isFollowComm: res.data.data.community.isFollowComm,
          }, function () {
            wx.setNavigationBarTitle({
              title: that.data.community.name
            })
          })
        }, 500)
      }
    });
  },

  // 滚动切换标签样式
  switchTab: function (e) {
    console.log(e.detail.current)
    this.setData({
      currentTab: e.detail.current,
      page_index: 1,
      areas: [],
      activeData: [],
      loading: false,
      isBottom: false,
      scrollTop: 0
    })
    this.loadMoreData()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    $Login();
  },

  /** 取消/添加关注 */
  addFollow: function () {
    let that = this;
    let sendData = {
      community_id: this.data.communityId,
      open_id: app.globalData.wxUserInfo.openId
    }
    console.log('isFollowComm  -- >' + that.data.isFollowComm)
    if (that.data.isFollowComm == 0) {
      wx.request({
        url: api.follow.addFollow,
        data: sendData,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success(res) {
          if (res.data.code === 200) {
            $Toast({
              content: '关注成功',
              type: 'success'
            });
            that.setData({
              isFollowComm: 1
            })
          } else {
            $Toast({
              content: '取消关注',
              type: 'error'
            });
          }
        }
      });
    } else {
      wx.request({
        url: api.follow.delFollow,
        data: sendData,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success(res) {
          if (res.data.code === 200) {
            $Toast({
              content: '取消关注',
              type: 'warning'
            });
            that.setData({
              isFollowComm: 0
            })
          } else {
            $Toast({
              content: '取消关注',
              type: 'error'
            });
          }
        }
      });
    }
  },
  /** 跳转到详情页面 */
  toDetail: function (e) {
    console.log(e)
    let type = e.currentTarget.dataset.listtype;
    let id = e.currentTarget.dataset.listid;
    if (type == 0) {
      wx.navigateTo({
        url: `../art/art?id=${id}`
      })
    } else if (type == 1) {
      wx.navigateTo({
        url: `../activedetial/activedetial?id=${id}`
      })
    } else if (type == 2) {
      wx.navigateTo({
        url: `../productDetail/productDetail?id=${id}`
      })
    }
  },
  toCompany: function () {
    wx.navigateTo({
      url: `../mineCompany/mineCompany?compId=${this.data.community.companyId}`
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  tell: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.community.companyPhone //仅为示例，并非真实的电话号码
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.community.name,
      path: `pages/community/community?communitiesId=${this.data.communityId}&shareId=${app.globalData.wxUserInfo.openId}`,
      success: function (res) {},
      fail: function (res) {}
    }
  },
  /** 图片预览功能 */
  preview_img: function (e) {
    console.log(e)
    let id = e.currentTarget.id;
    this.get_album_imgs(id)
    console.log()
    /** 获取相册图片列表 */
    // var imgList = ['http://wdtfile.v2too.com/2020/12/18/a1855cd6f7aad196fc6c971099b51703.jpg','http://wdtfile.v2too.com/2020/12/18/a1855cd6f7aad196fc6c971099b51703.jpg','http://wdtfile.v2too.com/2020/12/18/a1855cd6f7aad196fc6c971099b51703.jpg']; //获取data-list
    //图片预览
    setTimeout(() => {
      wx.previewImage({
        urls: this.data.imgarr
      })
    }, 500)

  },

  /** 获取相册的图片 */
  get_album_imgs(id) {
    let _this = this
    wx.request({
      url: api.community.album_imgs,
      data: {
        id: id
      },
      method: 'GET',
      success(res) {
        console.log(res.data.data)
        _this.setData({
          imgarr: res.data.data
        })
      }
    });
  },
})